<extend name="Layout/ins_page" />
<block name="content">
	{:W('PageHeader/simple',array('name'=>'发布任务','search'=>'N'))}
	<form method='post' id="form_data" name="form_data" enctype="multipart/form-data" class="well form-horizontal">
		
		<input type="hidden" id="ajax" name="ajax" value="0" />
		<input type="hidden" id="opmode" name="opmode" value="add" />
		
		<div class="form-group">
			<label class="col-sm-2 control-label" for="name">任务标题：</label>
			<div class="col-sm-10">
				<input class="form-control" type="text" id="name" name="name" value=""  check="require" msg="请输入标题" />
			</div>
		</div>
		<div class="form-group task-type-parent hidden">
			<label class="col-sm-2 control-label" for="name">任务类型：</label>
			<div class="col-sm-8">
				<input class="ace"  type="hidden" name="task_type" value="1" checked/>多流程
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label" for="name">流程数：</label>
			<div class="col-sm-10">
				<input class="form-control" type="text" id="task_num" name="task_num" value="1" check="require"  msg="流程数"  onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" onafterpaste="this.value=this.value.replace(/[^0-9]/g,'')" />
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label" for="name">生产产品：</label>
			<div class="add-task-product">
				<div class="col-sm-2">
					<select id="task-storehouse-product-id" name="task_storehouse_product_id"  class="form-control" check="require"  msg="请选择生产产品">
						<option value="">请选择</option>
						<foreach name="product" item="vo">
							<option value="{$vo.id}">{$vo.product}|{$vo.product_bh}</option>
						</foreach>
					</select>
					<input class="form-control task-product-check-in-number" type="text"  name="task_in_number" placeholder="数量" check="require"  msg="请输入数量" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" onafterpaste="this.value=this.value.replace(/[^0-9]/g,'')"/>
				</div>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label" for="name">附件：</label>
			<div class="col-sm-10">
				{:W('FileUpload/add')}
			</div>
		</div>
		<div class="parent-jj">
			
			<div class="jj">
				<hr style="height:1px;border-top:1px solid #555555;"/>
				<div class="form-group">
					<label class="col-sm-2 control-label" for="name">流程<span class="process-title">1</span>标题：</label>
					<div class="col-sm-10">
						<input class="form-control" type="text" id="process_name" name="process_name[]" value=""  check="require" msg="请输入标题" />
					</div>
				</div>
				<div class="form-group">
					<input type="hidden" id="executor" name="executor[]" value=""/>
			        <input type="hidden" id="in_check" name="in_check[]" value="" />
			        <input type="hidden" id="into_check_user_name" name="into_check_user_name[]" value=""/>
					<label class="col-sm-2 control-label" for="name">指派给：</label>
					<div class="col-sm-10">
						<div id="actor_wrap" class="inputbox">
							<a class="pull-right btn btn-link text-center popup-actor"><i class="fa fa-user"></i> </a>
							<div class="wrap" >
								<span class="address_list"></span>
								<span class="text" >
									<input class="letter" type="text"/>
								</span>
							</div>
							<div class="search dropdown ">
								<ul class="dropdown-menu"></ul>
							</div>
						</div>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label" for="expected_time">期望完成时间：</label>
					<div class="col-sm-10">
						<input class="form-control input-date-time" type="text" id="expected_time[]" name="expected_time[]" value="" readonly="readonly">
					</div>
				</div>
				
				<div class="form-group">
					<label class="col-sm-2 control-label" for="content">任务说明：</label>
					<div class="col-sm-10">
						<textarea id="content" name="content[]" style="width:100%;height:100px;"  msg="请输入内容"></textarea>
					</div>
				</div>
				<div class="form-group is-in-check">
					<label class="col-sm-2 control-label" for="name">是否入库：</label>
					<div class="col-sm-8">
						<span class="is-in-check-no hide"><input class="ace no"  type="radio"   value="0" name="is_check_in[0]" />否 &nbsp; &nbsp;</span>
						<span class="is-in-check-yes"><input class="ace yes"  type="radio" checked="checked"  value="1" name="is_check_in[0]" />是</span>
					</div>
				</div>
				<div class="form-group in-parent-into-check">
					<label class="col-sm-2 control-label" for="name">入库审核人：</label>
					<div class="col-sm-10">
						<div id="into_wrap" class="inputbox">
							<a class="pull-right btn btn-link text-center popup-into"><i class="fa fa-user"></i> </a>
							<div class="wrap" >
								<span class="address_list"></span>
								<span class="text" >
									<input class="letter" type="text"/>
								</span>
							</div>
							<div class="search dropdown">
								<ul class="dropdown-menu"></ul>
							</div>
						</div>
					</div>
				</div>
				<div class="form-group is-audit">
					<label class="col-sm-2 control-label" for="name">产品检验：</label>
					<div class="col-sm-8">
						<span class="is-no-audit-parent"><input class="ace is-no-audit"  name="is_audit[0]"  type="radio"  checked="checked" value="0"/>否 &nbsp; &nbsp;</span>
						<span class="is-yes-audit-parent"><input class="ace is-yes-audit"  type="radio" name="is_audit[0]"  value="1"/>是</span>
					</div>
				</div>
				
				<div class="form-group in-parent-check hide">
					<label class="col-sm-2 control-label" for="name">产品检验人：</label>
					<div class="col-sm-10">
						<div id="in_wrap" class="inputbox">
							<a class="pull-right btn btn-link text-center popup-in"><i class="fa fa-user"></i> </a>
							<div class="wrap" >
								<span class="address_list"></span>
								<span class="text" >
									<input class="letter" type="text"/>
								</span>
							</div>
							<div class="search dropdown">
								<ul class="dropdown-menu"></ul>
							</div>
						</div>
					</div>
				</div>
				<div class="form-group parent-add-task-product hide">
					<label class="col-sm-2 control-label" for="name">发布流程产品：</label>
					<div class="add-task-product">
						<div class="col-sm-2">
							<input class="form-control product-check-no" type="text"  name="product[0][]" placeholder="产品"   msg="请输入流程产品" />
							<input class="form-control product-check-no-number" type="text"  name="number[0][]" placeholder="数量"   msg="请输入数量" />
							<input class="form-control product-check-no-price" type="text"  name="product_price[0][]" placeholder="产品单价"  msg="请输入产品单价" />
						</div>
						<a class="btn btn-sm btn-primary product-add">添加</a>
					</div>
				</div>
				<div class="form-group parent-add-task-product-check-in">
					<label class="col-sm-2 control-label" for="name">流程产品：</label>
					<div class="add-task-product">
						<div class="col-sm-2">
							<select id="storehouse_product_id" name="storehouse_product_id[0][]"  class="form-control product-check-in" check="require" msg="请输入流程产品">
								<option value="">请选择</option>
								<foreach name="product" item="vo">
									<option value="{$vo.id}">{$vo.product}|{$vo.product_bh}</option>
								</foreach>
							</select>
							<input class="form-control product-check-in-number" type="text"  name="in_number[0][]" placeholder="数量"  msg="请输入数量" />
							<input class="form-control product-check-in-price" type="text"  name="in_product_price[0][]" placeholder="产品单价"  msg="请输入产品单价" />
						</div>
						<a class="btn btn-sm btn-primary product-add">添加</a>
					</div>
				</div>
		    </div>
		</div>
		<div class="form-group form-submit-button">
			<div class="action col-sm-10 col-sm-offset-2">
				<input class="btn btn-sm btn-primary" type="button" value="保存" onclick="save();" />
				<a class="btn btn-sm btn-default" type="button" href="javascript:;" onClick="javascript:history.go(-1);" />取消</a>
			</div>
		</div>
	</form>
</block>
<block name="js">
	<script type="text/javascript">
	
		function save() {
			
			var f = assign();
			var g = check();
			var i = into()
			if (!f || !g || !i){
				return false;
			}
			//sendForm("form_data", "{:U('save')}");
			sendForm("form_data", "{:U('operateSave')}");
		}
		
		function assign() {
			
			$(".jj").each(function(){
				$(this).find("#executor").val("");
			})
			var actorFlag = true;
			$(".jj").each(function(){
				var address = $(this).find("#actor_wrap span.address_list span");
				if (address.size()<=0) {
					actorFlag = false;
				}
			})
			if (!actorFlag) {
				ui_error('请选择指派人员');
				return false;
			}
			
			$(".jj").each(function() {
				//
				var executor = $(this).find("#executor");
				var address = $(this).find("#actor_wrap span.address_list span");
				address.each(function(){
					executor.val(executor.val() + $(this).find("b").text() + '|' + $(this).attr("data") + ";");	
				})
			});
			
			$(".jj").each(function(){
					
				$html = $(this).find("#executor").val();
				if ($html=="") {
					ui_error('请选择指派人员');
					return false;
				}
				var re = new RegExp("dept_", "g");
				var arr = $html.match(re);
				if (arr != null) {
					$dept_count = arr.length;
					$total_count = $html.split(';').length - 1;
					if ($dept_count != $total_count) {
						ui_error('部门和人员不能同时选择');
						return false;
					}
				}
			})
			return true;
		}
		
		function check() {
			
			$(".jj").each(function(){
				$(this).find("#in_check").val("");
			})
			
			$('.jj').each(function(){
				var _this = $(this);
				var is_audit = _this.find('.is-audit').find('input:checked').val();
				if (is_audit ==1) { 
					
					if (_this.find("#in_wrap span.address_list span").size() <=0){ 
						ui_error('请选择产品检验人员');
						return false;
					}
					
					if (_this.find("#in_wrap span.address_list span").size()>1){
						ui_error('产品检验人员有且只有一个');
						return false;
					}
					_this.find("#in_wrap span.address_list span").each(function() {
						var in_check = _this.find("#in_check");
						in_check.val(in_check.val() + $(this).find("b").text() + '|' + $(this).attr("data") + ";");
					})
					
					$html = _this.find("#in_check").val();
					if ($html=="") {
						ui_error('产品检验人员人员有且只有一个');
						_this.find("#in_check").attr('check','require');
						return false;
					}
					var re = new RegExp("dept_", "g");
					var arr = $html.match(re);
					if (arr != null) {
						ui_error('部门暂不能选择');
						return false;
					}
				}
			})
			return true;
		}
		
		/**
		 * 入库审核处理	
		 */
		function into() {
			
			$(".jj").each(function(){
				$(this).find("#into_check_user_name").val("");
			})	
			
			$('.jj').each(function(){
				var _this = $(this);
				var is_check_in = _this.find('.is-in-check').find('input:checked').val();
				if (is_check_in ==1) { 
					if (_this.find("#into_wrap span.address_list span").size() <=0){ 
						ui_error('请选择入库审核人员');
						return false;
					}
					_this.find("#into_wrap span.address_list span").each(function() {
						var into_check_user_name = _this.find("#into_check_user_name");
						into_check_user_name.val(into_check_user_name.val() + $(this).find("b").text() + '|' + $(this).attr("data") + ";");
					})
					$html = _this.find("#into_check_user_name").val();
					if ($html=="") {
						ui_error('入库审核必填');
						_this.find("#into_check_user_name").attr('check','require');
						return false;
					}
					var re = new RegExp("dept_", "g");
					var arr = $html.match(re);
					if (arr != null) {
						$dept_count = arr.length;
						if ($dept_count > 1) {
							ui_error('暂仅支持单部门');
							return false;	
						}
						return false;
					}
				}
			})
			return true;
		}
		
		function popup_actor() {
			//winopen("{:U('popup/task')}",560, 470);
			//alert($(this).html());
		}
		
		function popup_in() {
			//winopen("{:U('popup/in')}",560, 470);
		}
		
		$(document).ready(function() {
			
			$('.parent-jj').on('click','.is-audit .ace',function(event) {
				
				var jj = $(this).parents('.jj');
				if ($(this).val()==0) {
					jj.children('.in-parent-check').addClass('hide');
				} else {
					jj.children('.in-parent-check').removeClass('hide');
				}
				event.stopPropagation();
			})
			
			$('.parent-jj').on('click','.is-in-check .ace',function(event) {
				
				var jj = $(this).parents('.jj');
				if ($(this).val()==0) {
					jj.find('.parent-add-task-product').removeClass('hide');
					jj.find('.parent-add-task-product-check-in').addClass('hide');
					jj.find('.in-parent-into-check').addClass('hide');
					jj.find('.product-check-no').attr('check','require');
					jj.find('.product-check-in').removeAttr('check');
					
				} else {
					jj.find('.parent-add-task-product').addClass('hide');
					jj.find('.parent-add-task-product-check-in').removeClass('hide');
					jj.find('.in-parent-into-check').removeClass('hide');
					jj.find('.product-check-no').removeAttr('check');
					jj.find('.product-check-in').attr('check','require');
				}
				event.stopPropagation();
			})
			
			$(document).on("click", ".inputbox .address_list a.del", function() {
				$(this).parent().parent().remove();
			});
			
			$('.parent-jj').on('click','.parent-add-task-product .product-add',function(event){ //添加
				
				var jj = $(this).parents('.jj');
				var ide = $('.parent-jj .jj').index(jj);
				console.log('no-in:'+ide);
				var html = '<div class="form-group parent-add-task-product">'
						      +'<label class="col-sm-2 control-label" for="name"></label>'
						      +'<div class="add-task-product">'
						      +'<div class="col-sm-2">'
						      +'<input class="form-control product-check-no" type="text"  name="product['+ide+'][]" check="require" msg="请输入生产产品" placeholder="产品">'
						      +'<input class="form-control product-check-no-number" type="text"  name="number['+ide+'][]" placeholder="数量" />'
						      +'<input class="form-control product-check-no-price" type="text"  name="product_price['+ide+'][]" placeholder="产品单价"    msg="请输入产品单价" />'
						      +'</div>'
						      +'<span class="btn btn-sm btn-primary product-del">删除</span>'
						      +'</div>'
						      +'</div>';
				jj.find('.parent-add-task-product').after(html);
				event.stopPropagation();
				
			});
			
			$('.parent-jj').on('click','.parent-add-task-product-check-in .product-add',function(event){  // 添加
				
				var jj = $(this).parents('.jj');
				var select = jj.find('.product-check-in').html();
				var ide = $('.parent-jj .jj').index(jj);
				console.log('in:'+ide);
				var html = '<div class="form-group parent-add-task-product-check-in">'
				      +'<label class="col-sm-2 control-label" for="name"></label>'
				      +'<div class="add-task-product">'
				      +'<div class="col-sm-2">'
				      +'<select id="storehouse_product_id" name="storehouse_product_id['+ide+'][]" class="form-control product-check-in" check="require" msg="请输入生产产品" >'
				      +select
				      +'</select>'
				      +'<input class="form-control product-check-in-number" type="text"  name="in_number['+ide+'][]" placeholder="数量" />'
				      +'<input class="form-control product-check-in-price" type="text"  name="in_product_price['+ide+'][]" placeholder="产品单价" />'
				      +'</div>'
				      +'<span class="btn btn-sm btn-primary product-del">删除</span>'
				      +'</div>'
				      +'</div>';
				jj.find('.parent-add-task-product-check-in').after(html);
				event.stopPropagation();
			})
			
			$('#form_data').on('click','.product-del',function(event){ //删除
				$(this).parents('.form-group').remove();
				event.stopPropagation();
			})
			
			
			$('.parent-jj').on('click','.popup-actor',function(){
				
				var ide = $('.parent-jj .popup-actor').index(this) + 1;
				//console.log("{:U('popup/task')}"+'&ide='+ide);
				winopen("{:U('popup/task')}"+'&ide='+ide,560, 470);
			})
			
			$('.parent-jj').on('click','.popup-in',function(){
				
				var ide = $('.parent-jj .popup-in').index(this) + 1;
				winopen("{:U('popup/in')}"+'&ide='+ide,560, 470);
			})
			
			
			$('.parent-jj').on('click','.popup-into',function(){  // 入库审核
				
				var ide = $('.parent-jj .popup-into').index(this) + 1;
				winopen("{:U('popup/into')}"+'&ide='+ide,560, 470);
			})
			
			
			
			
			// 流程数的变化
			$("#task_num").on('change',function(){
				
				//var js = $('.form-submit-button').attr('js')
				var val = $(this).val();
				if (val==0) {
					ui_error('流程数必须大于0');	
					return false;
				}
				if (val>20) {
					ui_error('流程数必须小于20');	
					return false;
				}
				var clone = $('.jj:first').clone().html();
				var html = '<div class="jj">' + clone + '<div>'
				$(".jj:not('.jj:first')").remove();
				for (var i = 1 ; i < val ; i ++ ){
					$('.parent-jj').append(html);
				}
				$('.jj').each(function(key){
					
					var is_check_in_str = "is_check_in["+(key)+"]";
					var is_audit_str = "is_audit["+key+"]";
					$(this).find('.is-in-check').find('input').attr("name",is_check_in_str);
					$(this).find('.is-audit').find('input').attr("name",is_audit_str);
					
					$(this).find('.product-check-no').attr("name","product["+(key)+"][]");
					$(this).find('.product-check-no-number').attr("name","number["+(key)+"][]");
					$(this).find('.product-check-no-price').attr("name","product_price["+(key)+"][]");
					
					$(this).find('.product-check-in').attr("name","storehouse_product_id["+(key)+"][]");
					$(this).find('.product-check-in-number').attr("name","in_number["+(key)+"][]");
					$(this).find('.product-check-in-price').attr("name","in_product_price["+(key)+"][]");
					
					$(this).find('.process-title').text(key+1);
					
					if (key < (val-1)) { // 
						
						$(this).find('.is-in-check-no   .no').attr("checked","checked");
						$(this).find('.is-in-check-yes  .yes').removeAttr("checked");
						$(this).find('.is-in-check-no').removeClass('hide');
						$(this).find('.is-in-check-yes').addClass('hide');
						$(this).find('.in-parent-into-check').addClass('hide');
						$(this).find('.parent-add-task-product').removeClass('hide');
						$(this).find('.product-check-no').attr("check","require");
						$(this).find('.parent-add-task-product-check-in').addClass('hide');
						$(this).find('.product-check-in').removeAttr("check");
						$(this).find('.is-audit .is-no-audit').attr("checked","checked");
						$(this).find('.is-audit .is-yes-audit').removeAttr("checked");
						$(this).find('.in-parent-check').addClass('hide');
						
						
					} else if (key == (val-1)) {
						$(this).find('.is-in-check-no   .no').removeAttr("checked");
						$(this).find('.is-in-check-yes  .yes').attr("checked","checked");
						
						$(this).find('.is-in-check-no').addClass('hide');
						$(this).find('.is-in-check-yes').removeClass('hide');
						$(this).find('.in-parent-into-check').removeClass('hide');
						$(this).find('.parent-add-task-product').addClass('hide');
						$(this).find('.product-check-no').removeAttr("check");
						$(this).find('.parent-add-task-product-check-in').removeClass('hide');
						$(this).find('.product-check-in').attr("check","require");
						$(this).find('.is-audit .is-no-audit').attr("checked","checked");
						$(this).find('.is-audit .is-yes-audit').removeAttr("checked");
						$(this).find('.in-parent-check').addClass('hide');
						
						$(this).find('.is-in-check .ace').trigger('click');
					}
				})
				
				$(".input-date-time").datetimepicker({
					format : 'YYYY-MM-DD HH:mm',
					locale : 'zh-cn',
					sideBySide : true,
					showTodayButton : true,
					showClose : true,
					ignoreReadonly : true,
					widgetPositioning : {
						horizontal : 'auto',
						vertical : 'bottom'
					},
				});
			})
		});
	</script>
</block>
